package com.i2863.receipt.dao;

import com.i2863.receipt.entity.Winner;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2018/6/8.
 */
public interface WinnerRepository extends JpaRepository<Winner,Integer>,JpaSpecificationExecutor<Winner>{

//    @Query(value = "select * from d_winner where win_phonenum = :phoneNum and site_id = :siteId",nativeQuery = true)
//    List<Winner> findByPhoneNumAndSiteId(@Param("phoneNum")String phoneNum,@Param("siteId")Integer siteId,Integer start,Integer end);


    List<Winner> findByActivityIdAndIsReselection(Integer activityId,boolean isReselection);

    @Modifying
    @Query(value = "update d_winner  set is_inside = true,money=200,level_name='场内奖'  where id in (:ids)",nativeQuery = true)
    Integer updateInsideByIds(@Param(value = "ids") List<Integer> idList);

    @Modifying
    @Query(value="update d_winner  set is_inside = false,money=100,level_name='场外奖' where activity_id=:id and  is_reselection=false and is_inside is null ",nativeQuery = true)
    Integer updateOutSideById(@Param(value = "id") Integer id);

    @Modifying
    @Query(value = "update d_winner set level=:level,level_name=:levelName,money=:money,prize=:prize,prize_name=:prizeName where receipt_code=:receiptCode and activity_id=:activityId",nativeQuery = true)
    void updateWinnerData(@Param("level")Integer level,@Param("levelName")String levelName,
                          @Param("money")BigDecimal money,@Param("prize")Integer prize,
                          @Param("prizeName")String prizeName,@Param("receiptCode")String receiptCode,
                          @Param("activityId")Integer activityId);

    /**
     * 查询场内选手
     * @param activityId  活动id
     * @return 场内选手
     */
    List<Winner> findByActivityIdAndIsInsideTrue(Integer activityId);

    /**
     * 查询场外选手
     * @param activityId 活动id
     * @return 场外选手
     */
    List<Winner> findByActivityIdAndIsInsideFalse(Integer activityId);

    Integer countByActivityIdAndLevelAndIsReselection(Integer activityId,Integer level,boolean isReselection);

    /**
     *   查询场内已经中奖的选手
     * @param activityId 活动id
     * @return 已经中奖的场内选手
     */
    List<Winner> findByActivityIdAndIsReselectionFalseAndLevelNotNull(Integer activityId);

    /**
     * 查询场外已经中奖的选手
     * @param activityId
     * @return 已经中奖的场外选手
     */
    List<Winner> findByActivityIdAndIsReselectionTrueAndLevelNotNull(Integer activityId);

    /**
     * 查询未中奖的场内用户
     * @param activityId 活动id
     * @return 未中奖的场内用户
     */
    List<Winner> findByActivityIdAndIsInsideTrueAndLevelIsNull(Integer activityId);

    /**
     * 查询未中奖的场外用户
     * @param activityId 活动id
     * @return 未中奖的场外用户
     */
    List<Winner> findByActivityIdAndIsReselectionTrueAndLevelIsNull(@Param("activityId") Integer activityId);

    /**
     * 写入多个中奖纪录
     * @param idList 中奖用户id列表
     * @param money 奖金数量
     * @param level 中奖等级
     * @param levelName  奖品名称
     * @param prize  奖项等级
     * @param prizeName 奖项名称
     * @return  更新记录数
     */
    @Modifying
    @Query(value = "update  d_winner set money = :money ,level = :level,level_name = :levelName,prize = :prize ,prize_name = :prizeName  where  id in (:ids)",nativeQuery = true)
    Integer updatePrizeByIdList(@Param("ids") List<Integer> idList, @Param("money") BigDecimal money, @Param("level") Integer level, @Param("levelName") String levelName,
                                @Param("prize") Integer prize, @Param("prizeName")String prizeName);


    /**
     *  写入单个中奖记录
     * @param id  中奖者id
     * @param money 奖金金额
     * @param level 中奖等级
     * @param levelName  奖品内容
     * @param prize  奖项登记
     * @param prizeName 奖项内容
     * @return 更新记录数
     */
    @Modifying
    @Query(value = "update  d_winner set money = :money ,level = :level,level_name = :levelName,prize = :prize ,prize_name = :prizeName  where  id =:id",nativeQuery = true)
    Integer updatePrizeById(@Param("id") Integer id, @Param("money") BigDecimal money, @Param("level") Integer level, @Param("levelName") String levelName,
                                @Param("prize") Integer prize, @Param("prizeName")String prizeName);

    /**
     * 查询特定活动中了特定奖项的场内或场外用户
     * @param activityId 活动id
     * @param isReselection 是否复活用户
     * @param level 级别
     * @return 中奖用户
     */
    List<Winner>  findByActivityIdAndIsReselectionAndLevel(Integer activityId,boolean isReselection,Integer level);

    /**
     * 查看指定奖项的中奖用户
     * @param activityId 活动id
     * @param level 奖项等级
     * @return 中奖用户
     */
    List<Winner> findByActivityIdAndLevel(Integer activityId,Integer level);

    /**
     * 查看指定奖项和范围的中奖用户
     * @param activityId 活动id
     * @param level 奖项等级
     * @param isReselection 是否复活赛
     * @return 中奖用户
     */
    List<Winner> findByActivityIdAndLevelAndIsReselection(Integer activityId,Integer level,boolean isReselection);

    @Modifying
    @Query(value="insert into d_winner(createDate,modifyDate,is_reselection,win_phonenum,activity_id,receipt_code,receipt_codenum,receipt_money,receipt_date,site_id)  select now(),now(),:isReselection,phonenum ,activity_id, `code`,code_num,money,receipt_date,:siteId   from d_receipt where  id in (:ids)",nativeQuery = true)
    Integer  batchInsert(@Param("ids") List<Integer> ids,@Param("siteId") Integer siteId,@Param("isReselection") boolean isReselection);

    @Modifying
    @Query
    void deleteByActivityId(Integer activityId);

    @Modifying
    @Query(value="update d_winner set is_inside=NULL, `level`=NULL,  level_name=NULL, money=NULL, prize=NULL, prize_name=NULL  where activity_id=:activityId",nativeQuery = true)
    void resetInside(@Param("activityId") Integer activityId);

    @Modifying
    @Query(value="update d_winner set  `level`=NULL,  level_name=NULL, money=NULL, prize=NULL, prize_name=NULL  where activity_id=:activityId",nativeQuery = true)
    void resetLottery(@Param("activityId") Integer activityId);

    /**
     * 按发票号码和活动id查询中奖
     * @param receiptCode
     * @param activityId
     * @return
     */
    @Query(value = "select * from d_winner where receipt_code=:receiptCode and activity_id=:activityId",nativeQuery = true)
    Winner findByActivityAndCode(@Param("receiptCode")String receiptCode,@Param("activityId")Integer activityId);
}